package com.by.zhdoctor.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.by.zhdoctor.domain.D_m_zhenghe_user_stat;

import com.by.zhdoctor.repository.D_m_zhenghe_user_statRepository;
import com.by.zhdoctor.web.rest.util.HeaderUtil;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;
import javax.validation.Valid;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing D_m_zhenghe_user_stat.
 */
@RestController
@RequestMapping("/api")
@Api(tags= "用户量统计表")
public class D_m_zhenghe_user_statResource {

    private final Logger log = LoggerFactory.getLogger(D_m_zhenghe_user_statResource.class);

    @Inject
    private D_m_zhenghe_user_statRepository d_m_zhenghe_user_statRepository;

    /**
     * POST  /d-m-zhenghe-user-stats : Create a new d_m_zhenghe_user_stat.
     *
     * @param d_m_zhenghe_user_stat the d_m_zhenghe_user_stat to create
     * @return the ResponseEntity with status 201 (Created) and with body the new d_m_zhenghe_user_stat, or with status 400 (Bad Request) if the d_m_zhenghe_user_stat has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/d-m-zhenghe-user-stats")
    @Timed
    public ResponseEntity<D_m_zhenghe_user_stat> createD_m_zhenghe_user_stat(@Valid @RequestBody D_m_zhenghe_user_stat d_m_zhenghe_user_stat) throws URISyntaxException {
        log.debug("REST request to save D_m_zhenghe_user_stat : {}", d_m_zhenghe_user_stat);
        if (d_m_zhenghe_user_stat.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert("d_m_zhenghe_user_stat", "idexists", "A new d_m_zhenghe_user_stat cannot already have an ID")).body(null);
        }
        D_m_zhenghe_user_stat result = d_m_zhenghe_user_statRepository.save(d_m_zhenghe_user_stat);
        return ResponseEntity.created(new URI("/api/d-m-zhenghe-user-stats/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert("d_m_zhenghe_user_stat", result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /d-m-zhenghe-user-stats : Updates an existing d_m_zhenghe_user_stat.
     *
     * @param d_m_zhenghe_user_stat the d_m_zhenghe_user_stat to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated d_m_zhenghe_user_stat,
     * or with status 400 (Bad Request) if the d_m_zhenghe_user_stat is not valid,
     * or with status 500 (Internal Server Error) if the d_m_zhenghe_user_stat couldnt be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/d-m-zhenghe-user-stats")
    @Timed
    public ResponseEntity<D_m_zhenghe_user_stat> updateD_m_zhenghe_user_stat(@Valid @RequestBody D_m_zhenghe_user_stat d_m_zhenghe_user_stat) throws URISyntaxException {
        log.debug("REST request to update D_m_zhenghe_user_stat : {}", d_m_zhenghe_user_stat);
        if (d_m_zhenghe_user_stat.getId() == null) {
            return createD_m_zhenghe_user_stat(d_m_zhenghe_user_stat);
        }
        D_m_zhenghe_user_stat result = d_m_zhenghe_user_statRepository.save(d_m_zhenghe_user_stat);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert("d_m_zhenghe_user_stat", d_m_zhenghe_user_stat.getId().toString()))
            .body(result);
    }

    /**
     * GET  /d-m-zhenghe-user-stats : get all the d_m_zhenghe_user_stats.
     *
     * @return the ResponseEntity with status 200 (OK) and the list of d_m_zhenghe_user_stats in body
     */
    @GetMapping("/d-m-zhenghe-user-stats")
    @Timed
    public List<D_m_zhenghe_user_stat> getAllD_m_zhenghe_user_stats() {
        log.debug("REST request to get all D_m_zhenghe_user_stats");
        List<D_m_zhenghe_user_stat> d_m_zhenghe_user_stats = d_m_zhenghe_user_statRepository.findAll();
        return d_m_zhenghe_user_stats;
    }

    /**
     * GET  /d-m-zhenghe-user-stats/:id : get the "id" d_m_zhenghe_user_stat.
     *
     * @param id the id of the d_m_zhenghe_user_stat to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the d_m_zhenghe_user_stat, or with status 404 (Not Found)
     */
    @GetMapping("/d-m-zhenghe-user-stats/{id}")
    @Timed
    public ResponseEntity<D_m_zhenghe_user_stat> getD_m_zhenghe_user_stat(@PathVariable Long id) {
        log.debug("REST request to get D_m_zhenghe_user_stat : {}", id);
        D_m_zhenghe_user_stat d_m_zhenghe_user_stat = d_m_zhenghe_user_statRepository.findOne(id);
        return Optional.ofNullable(d_m_zhenghe_user_stat)
            .map(result -> new ResponseEntity<>(
                result,
                HttpStatus.OK))
            .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    /**
     * DELETE  /d-m-zhenghe-user-stats/:id : delete the "id" d_m_zhenghe_user_stat.
     *
     * @param id the id of the d_m_zhenghe_user_stat to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/d-m-zhenghe-user-stats/{id}")
    @Timed
    public ResponseEntity<Void> deleteD_m_zhenghe_user_stat(@PathVariable Long id) {
        log.debug("REST request to delete D_m_zhenghe_user_stat : {}", id);
        d_m_zhenghe_user_statRepository.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert("d_m_zhenghe_user_stat", id.toString())).build();
    }

}
